package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.*;
import com.example.zhunongzonghe.model.EchartsCount;
import com.example.zhunongzonghe.model.EchartsModel;
import com.example.zhunongzonghe.service.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Resource
    private IZnSortService iZnSortService;
    @Resource
    private IZnFarmproduceService iZnFarmproduceService;
    @Resource
    private IZnOrderService znOrderService;
    @Resource
    private IZnUsersService usersService;
    @Resource
    private IZnFarmhouseReserveService reserveService;
    /**
     * 后台首页饼图
     * @return
     */
    @RequestMapping("/getFarmPiz")
    public Result getFarmPiz(){
        List<ZnSort> sortList = iZnSortService.list();
        // 创建一个 Map 来存储每个分类的销量总和
        List<Map<Object, Object>> salesSumBySort = new ArrayList<>();
        sortList.forEach(sort->{
            LambdaQueryWrapper<ZnFarmproduce> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZnFarmproduce::getSortId,sort.getSortId());
            List<ZnFarmproduce> list = iZnFarmproduceService.list(queryWrapper);
            // 计算该分类下的销量总和
            int salesSum = list.stream().mapToInt(ZnFarmproduce::getSalesVolume).sum();
            Map<Object, Object> salesSumMap = new HashMap<>();
            salesSumMap.put("value",salesSum);
            salesSumMap.put("name",sort.getSortContent());
            salesSumBySort.add(salesSumMap);
        });
        return new Result(true,"成功",200,salesSumBySort);
    }

    /**
     * 后台首页日订单折线图
     * @return
     */
    @RequestMapping("/getFarmLine")
    public Result getFarmLine(){
        List<Object> getFarmLine = new ArrayList<>();
        List<String> orderDateList = new ArrayList<>();
        List<Long> orderCountList = new ArrayList<>();
        List<BigDecimal> totalPriceList = new ArrayList<>();
        List<EchartsModel> echartsList = new ArrayList<>();
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
        for (int i = 0; i <= 6; i++) {
            LocalDate date = today.minusDays(i);
            LocalDateTime startOfDay = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endOfDay = LocalDateTime.of(date, LocalTime.MAX);
            LambdaQueryWrapper<ZnOrder> dayWrapper = new LambdaQueryWrapper<>();
            dayWrapper.between(ZnOrder::getCreateTime, startOfDay, endOfDay)
                    .eq(ZnOrder::getPayStatus,"1");
            List<ZnOrder> znOrder = znOrderService.list(dayWrapper);
            BigDecimal totalPrice = BigDecimal.ZERO;
            for (ZnOrder order : znOrder) {
                totalPrice = totalPrice.add(order.getTotalPrice());
            }
            long orderCount = znOrder.size();
            String formattedDate = date.format(formatter);
            echartsList.add(new EchartsModel(formattedDate,orderCount,totalPrice));
        }
        Collections.reverse(echartsList);
        for (EchartsModel echartsModel : echartsList) {
            orderDateList.add(echartsModel.getDateTime());
            orderCountList.add(echartsModel.getCount());
            totalPriceList.add(echartsModel.getTotalPrice());
        }
        getFarmLine.add(orderDateList);
        getFarmLine.add(orderCountList);
        getFarmLine.add(totalPriceList);
        return new Result(true,"成功",200,getFarmLine);
    }

    /**
     * 后台首页月订单数折线图
     * @return
     */
    @RequestMapping("/getFarmMonthLine")
    public Result getFarmMonthLine(){
        List<Object> farmMonthLine = new ArrayList<>();
        List<String> monthList = new ArrayList<>();
        List<Long> orderCountList = new ArrayList<>();
        List<EchartsModel> echartsList = new ArrayList<>();
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        for (int i = 0; i < 6; i++) {
            LocalDate startOfMonth = today.withDayOfMonth(1).minusMonths(i);
            LocalDate endOfMonth = startOfMonth.with(TemporalAdjusters.lastDayOfMonth());
            LambdaQueryWrapper<ZnOrder> monthWrapper = new LambdaQueryWrapper<>();
            monthWrapper.between(ZnOrder::getCreateTime, startOfMonth.atStartOfDay(),
                    endOfMonth.atTime(LocalTime.MAX))
                    .eq(ZnOrder::getPayStatus,"1");
            long orderCount = znOrderService.count(monthWrapper);
            String formattedMonth = startOfMonth.format(formatter);
            echartsList.add(new EchartsModel(formattedMonth, orderCount, BigDecimal.ZERO));
        }
        Collections.reverse(echartsList);
        for (EchartsModel echartsModel : echartsList) {
            monthList.add(echartsModel.getDateTime());
            orderCountList.add(echartsModel.getCount());
        }
        farmMonthLine.add(monthList);
        farmMonthLine.add(orderCountList);
        return new Result(true,"成功",200,farmMonthLine);
    }
    @RequestMapping("/getNewUserSum")
    public Result getNewUserSum(){
        EchartsCount echartsCount = new EchartsCount();
        LambdaQueryWrapper<ZnUsers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(ZnUsers::getCreateTime,
                LocalDateTime.of(LocalDate.now(), LocalTime.MIN),LocalDateTime.of(LocalDate.now(), LocalTime.MAX))
                .eq(ZnUsers::getRole,"user");
        Long newCount = usersService.count(queryWrapper);
        LambdaQueryWrapper<ZnUsers> countQueryWrapper = new LambdaQueryWrapper<>();
        countQueryWrapper.eq(ZnUsers::getRole,"user");
        Long count = usersService.count(countQueryWrapper);
        echartsCount.setNewCount(newCount);
        echartsCount.setCount(count);
        return new Result(true,"成功",200, echartsCount);
    }
    @RequestMapping("/getNewOrderSum")
    public Result getNewOrderSum(){
        EchartsCount echartsCount = new EchartsCount();
        LambdaQueryWrapper<ZnOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(ZnOrder::getCreateTime,
                        LocalDateTime.of(LocalDate.now(), LocalTime.MIN),
                        LocalDateTime.of(LocalDate.now(), LocalTime.MAX))
                .eq(ZnOrder::getPayStatus,"1");
        Long newCount = znOrderService.count(queryWrapper);
        Long count = znOrderService.count();
        echartsCount.setNewCount(newCount);
        echartsCount.setCount(count);
        return new Result(true,"成功",200, echartsCount);
    }
    @RequestMapping("/getNewHouseReserve")
    public Result getNewHouseReserve(){
        EchartsCount echartsCount = new EchartsCount();
        LambdaQueryWrapper<ZnFarmhouseReserve> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(ZnFarmhouseReserve::getCreateTime,
                        LocalDateTime.of(LocalDate.now(), LocalTime.MIN),
                        LocalDateTime.of(LocalDate.now(), LocalTime.MAX))
                .eq(ZnFarmhouseReserve::getPayStatus,"1");
        Long newCount = reserveService.count(queryWrapper);
        Long count = reserveService.count();
        echartsCount.setNewCount(newCount);
        echartsCount.setCount(count);
        return new Result(true,"成功",200, echartsCount);
    }
}
